package com.medical.system.service.impl;

import com.medical.system.common.Result;
import com.medical.system.service.LoginService;
import com.medical.system.service.SysUserService;
import com.medical.system.util.JwtHelper;
import com.medical.system.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    SysUserService sysUserService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result login(LoginVo loginVo) {
        return Result.ok();
    }

    @Override
    public Result loginInfo(String token) {
        //b.从token 字符串获取用户名称 (id)  这一步其实就是在校验jwt的token是否合法！！！
        String username = JwtHelper.getUsername(token);
        // c.根据用户名称获取用户信息 （a.基本信息  b.菜单权限信息  和 c.按钮权限信息）
        Map<String, Object> map = sysUserService.getUserInfo(username);
        return Result.ok(map);
    }

    @Override
    public Result logout() {
//        //获取SecurityContextHolder中的用户id
//        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
//        CustomUser customUser = (CustomUser) authentication.getPrincipal();
////        删除
//        redisTemplate.delete("username:" + customUser.getUsername());
        return Result.ok();
    }
}
